package com.csf.service.impl;

import com.csf.config.likeKeyConfig;
import com.csf.service.DateService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.dao.DataAccessException;
import org.springframework.data.redis.connection.RedisConnection;
import org.springframework.data.redis.connection.RedisStringCommands;
import org.springframework.data.redis.core.RedisCallback;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

@Service
public class DateServiceImpl implements DateService {

    @Autowired
    private RedisTemplate redisTemplate;

    private SimpleDateFormat format = new SimpleDateFormat("yyyyMMdd");

    @Override
    public void saveUV(String ip) {
        String format = this.format.format(new Date());
        String key = likeKeyConfig.getUAkey(format);
        redisTemplate.opsForHyperLogLog().add(key,ip);
    }

    @Override
    public long selectUV(Date start, Date end) {
//        判断是否为空
        if (start ==null || end == null){
            throw new IllegalArgumentException("参数不能为空");
        }
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(start);
        List<String> list = new ArrayList<>();
//        利用while循环判断时间区间的差值
        while (!calendar.getTime().after(end)){
            String keyUV = likeKeyConfig.getUAkey(format.format(calendar.getTime()));
            list.add(keyUV);
//            如果当前记录的时间小于最后的时间，则加一
            calendar.add(calendar.DATE,1);
        }
        String keyUA = likeKeyConfig.getUAkey(format.format(start),format.format(end));
        redisTemplate.opsForHyperLogLog().union(keyUA, list.toArray());
//返回统计结果
        return redisTemplate.opsForHyperLogLog().size(keyUA);
    }

    @Override
    public void saveDAU(int userId) {
//        对签到用户进行存储
        String dayTime = this.format.format(new Date());
        String dauKey=likeKeyConfig.getDAUkey(dayTime);
        redisTemplate.opsForValue().setBit(dauKey,userId,true);
    }

    @Override
    public long selectDAU(Date start, Date end) {
        if (start ==null || end ==null){
            throw new IllegalArgumentException("数据不能为空");
        }
        //首先得到时间区间里的各个时间
        List<byte[]> list = new ArrayList<>();
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(start);
        while (!calendar.getTime().after(end)){
            String dauKey = likeKeyConfig.getDAUkey(format.format(calendar.getTime()));
            list.add(dauKey.getBytes());
            calendar.add(Calendar.DATE,1);
        }
        String keydau = likeKeyConfig.getDAUkey(format.format(start),format.format(end));
        return (Long) redisTemplate.execute(new RedisCallback() {
            @Override
            public Object doInRedis(RedisConnection connection) throws DataAccessException {
                connection.bitOp(RedisStringCommands.BitOperation.OR, keydau.getBytes(), list.toArray(new byte[0][0]));
                return connection.bitCount(keydau.getBytes());
            }
        });
    }
}
